package dk.netl.dashboard.widgets;

import javafx.scene.shape.Rectangle;
import javafx.scene.control.Button;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;

public class DashboardWidget extends Group {

    public var x: Number;
    public var y: Number;
    public var width: Number;
    public var height: Number;
    var orgX: Number;
    var orgY: Number;
    var orgWidth: Number;
    var orgHeight: Number;
    override var onMouseEntered = function (e) {
       fadeTransitionOut.stop();
       fadeTransitionIn.playFromStart();
    };
    override var onMouseExited = function (e) {
       fadeTransitionIn.stop();
       fadeTransitionOut.playFromStart();
    };
    def maximizeButton: Button = Button {
                text: "Maximize"
                layoutX: bind this.width - 70
                action: function() {
                    maxTransition.playFromStart();
                }
            }
    def closeButton: Button = Button {
                text: "Minimize"
                layoutX: bind this.width - 140
                action: function() {
                    minTransition.playFromStart();
                }
            }
    def headerPanel: Group = Group {
                opacity: 0
                content: [
                    maximizeButton,
                    closeButton
                ]
            }

    init {
        orgX = x;
        orgY = y;
        orgWidth = width;
        orgHeight = height;

        content = [
                    Rectangle {
                        opacity: 0.5
                        fill: Color.AQUAMARINE
                        x: bind x
                        y: bind y
                        width: bind width
                        height: bind height
                    },
                    headerPanel
                ]
    }

    def fadeTransitionIn = Timeline {
                keyFrames: [
                    KeyFrame {
                        time: 1s
                        values: [
                            headerPanel.opacity => 1.0
                        ]
                    }
                ]
            }
    def fadeTransitionOut = Timeline {
                keyFrames: [
                    KeyFrame {
                        time: 1s
                        values: [
                            headerPanel.opacity => 0.0
                        ]
                    }
                ]
            }
    def maxTransition = Timeline {
                keyFrames: [
                    KeyFrame {
                        time: 1s
                        values: [
                            this.width => scene.width,
                            this.height => scene.height,
                            this.x => 0,
                            this.y => 0
                        ]
                    }
                ]
            }
    def minTransition = Timeline {
                keyFrames: [
                    KeyFrame {
                        time: 1s
                        values: [
                            this.width => orgWidth,
                            this.height => orgHeight,
                            this.x => orgX,
                            this.y => orgY
                        ]
                    }
                ]
            }
}
